#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sys/time.h"
#include "time.h"
#include <cblas.h>

void dgemm(int m, int n, int k, int alpha, int beta,
          double *A, double *B, double *C){
    for(int i=0; i< m;i ++){    //C[i] 
        for(int j=0; j< n; j++){  //C[i][j]
            C[i * n + j] = beta*C[i * n +j];
            for(int p=0; p< k; p++){  
                C[i * n + j] += alpha * A[i * n +p]*B[p * n + j]; 
             }
        }
    }
}

int g_x;
int 
main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("Input Error\n");
        return 1;
    }

    printf("test!\n");
    int i, m, n, k;
    m = n = k = atoi(argv[1]);

    int sizeofa = m * k;
    int sizeofb = k * n;
    int sizeofc = m * n;
    int lda = m;
    int ldb = k;
    int ldc = m;

    double alpha = 1.2;
    double beta = 0.001;
    struct timeval start, finish;
    double duration;

    double *A = (double *)malloc(sizeof(double) * sizeofa);
    double *B = (double *)malloc(sizeof(double) * sizeofb);
    double *C = (double *)malloc(sizeof(double) * sizeofc);

    int main_x=0;
    printf("hello world (pid:%d)\n\n", (int) getpid());
    int rc = fork();
    if (rc < 0) {
        fprintf(stderr, "fork failed\n");
        exit(1);
    } else if (rc == 0) {
        printf("m=%d,n=%d,k=%d,alpha=%lf,beta=%lf,sizeofc=%d\n", m, n, k, alpha, beta, sizeofc);
        dgemm(m, n, k, alpha, beta, A, B, C);
    } else {
        printf("m=%d,n=%d,k=%d,alpha=%lf,beta=%lf,sizeofc=%d\n", m, n, k, alpha, beta, sizeofc);
        dgemm(m, n, k, alpha, beta, A, B, C);
    }

    free(A);
    free(B);
    free(C);
    return 0;
}